<template>
  <card>
    <go-back :content="title + '公告'" @back="goBack"/>
    <el-form :model="form" :rules="rules" ref="Form" label-width="100px">
      <row-col>
        <el-form-item label="标题(中文)" prop="title">
          <el-input v-model="form.title"></el-input>
        </el-form-item>
        <el-form-item slot="r" label="标题(老挝)" prop="laosTitle">
          <el-input v-model="form.laosTitle"></el-input>
        </el-form-item>
      </row-col>
      <el-form-item label="排序" prop="sort">
        <el-input-number
            v-model="form.sort"
            controls-position="right"
            :precision="0"
            :min="1">
        </el-input-number>
      </el-form-item>
      <el-tabs v-model="activeName" type="card">
        <el-tab-pane label="内容(中文)" name="first">
          <custom-editor :editor-key="2" v-model="form.content"></custom-editor>
        </el-tab-pane>
        <el-tab-pane label="内容(老挝)" name="second">
          <custom-editor :editor-key="3" v-model="form.laosContent"></custom-editor>
        </el-tab-pane>
      </el-tabs>
    </el-form>
    <div slot="footer">
      <submit-button ref="Submit" @submit="submit"/>
    </div>
  </card>
</template>

<script>
  import CustomEditor from '@/components/CustomEditor'
  import {editArticleApi, addArticleApi} from '@/api/website/article'
  import {resetForm} from "@/utils/common";

  export default {
    name: "NoticeDetail",
    components: {CustomEditor},
    data() {
      return {
        title: '',
        activeName: 'first',
        id: 0,
        form: {
          title: '',
          laosTitle: '',
          content: '',
          laosContent: '',
          typeName: '',
          sort: 9999,
          typeId: 2 // 1新闻 2公告
        },
        rules: {
          title: {required: true, message: '请输入标题', trigger: 'blur'},
          laosTitle: {required: true, message: '请输入标题', trigger: 'blur'},
          sort: {required: true, message: '请输入排序', trigger: 'blur'}
        }
      }
    },
    methods: {
      submit() {
        this.$refs['Form'].validate((valid) => {
          if (!valid) return false;
          let data = {...this.form};
          this.$refs.Submit.start();
          if (this.title === '新增') {
            addArticleApi(data).then(() => {
              this.$refs.Submit.stop();
              this.$emit('update');
              this.goBack()
            });
          } else {
            data.id = this.id;
            editArticleApi(data).then(() => {
              this.$refs.Submit.stop();
              this.$emit('update');
              this.goBack()
            });
          }
        });
      },
      goBack() {
        this.$parent.isShow = true;
        resetForm(this)
      }
    }
  }
</script>
